package org.four.web.controller;

import com.netflix.ribbon.proxy.annotation.Http;
import org.bouncycastle.cert.ocsp.Req;
import org.four.common.CommonConf;
import org.four.common.model.*;
import org.four.common.utils.Md5Util;
import org.four.common.utils.OSSClientUtil;
import org.four.web.service.ConpaynService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("company")
public class ConpanyController {

    @Autowired
    private ConpaynService conpaynService;

    //公司：账号登陆
    @RequestMapping("login")
    @ResponseBody
    public Map login(HttpSession session, int code, ComUserBean comUserBean){
        //总返回
        HashMap<String, Object> result = new HashMap<>();
        //获取验证码
        Object attribute = session.getAttribute("imgcode");
        if(attribute==null) {
            result.put("code", 1);
            result.put("msg", "验证码错误");
            return result;
        }
        //判断session中的验证码和输入的验证码是否一致
        int imgCode = Integer.parseInt(attribute.toString());
        if(imgCode!=code) {
            result.put("code", 1);
            result.put("msg", "验证码错误");
            return result;
        }
        //判断账户
        ComUserBean userInfo = conpaynService.findUserInfoByAccount(comUserBean.getUserName());
        if (userInfo == null) {
            result.put("code", 2);
            result.put("msg", "账号错误");
            return result;
        }

        //判断密码
        String md532 = Md5Util.getMd532(comUserBean.getPassWord());
        if (!userInfo.getPassWord().equals(md532)) {
            result.put("code",4);
            result.put("msg","账号或者密码错误");
            return result;
        }
        if (userInfo.getStatus() != 2 ) {
            result.put("code",4);
            result.put("msg","该账号正在审核中，暂时无法登陆");
            return result;
        }
        if (userInfo.getStatus() == 3 ) {
            result.put("code",4);
            result.put("msg","该账号已被冻结，暂时无法登陆,请联系工作人员 进行询问");
            return result;
        }
        if (userInfo.getStatus() == 5 ) {
            result.put("code",4);
            result.put("msg","该账号申请失败,请联系工作人员 进行询问");
            return result;
        }
        result.put("code", 0);
        result.put("msg", "登陆成功");
        session.setAttribute(session.getId(),userInfo);
        session.setAttribute("account",userInfo.getUserName());
        session.setAttribute("comName",userInfo.getComName());
        return result;
    }

    //注册
    @RequestMapping("regedit")
    @ResponseBody
    public Map regedit(ComUserBean comUserBean,String pwd1) {
        Map regedit = conpaynService.regedit(comUserBean, pwd1);
        return regedit;
    }

    //完善信息:修改回显
    @RequestMapping("addDatum")
    @ResponseBody
    public ComUserBean addDatum(HttpSession session){
        ComUserBean attribute = (ComUserBean) session.getAttribute(session.getId());
        Integer id = attribute.getId();
        return conpaynService.addDatum(id);
    }

    //账号信息
    @RequestMapping("companyMessage")
    @ResponseBody
    public ResultBean companyMessage(HttpSession session){
        ComUserBean attribute = (ComUserBean) session.getAttribute(session.getId());
        Integer id = attribute.getId();
        return conpaynService.companyMessage(id);
    }

    //完善信息:修改
    @RequestMapping("saveCompanyMessage")
    @ResponseBody
    public ComUserBean saveCompanyMessage(ComUserBean comUserBean){
        return conpaynService.saveCompanyMessage(comUserBean);
    }

    //修改密码
    @RequestMapping("editPassWord")
    @ResponseBody
    public Map editPassWord(HttpSession session,String passWord,String pwd1,String pwd2){
        ComUserBean attribute = (ComUserBean) session.getAttribute(session.getId());
        String passWord2 = attribute.getPassWord();
        Integer id = attribute.getId();
        HashMap<String, Object> result = new HashMap<>();
        if(!passWord2.equals(Md5Util.getMd532(passWord))) {
            result.put("code", 4);
            result.put("msg", "旧密码输入错误");
            return result;
        }
        if (pwd1 == null || pwd1 ==""){
            result.put("code", 1);
            result.put("msg", "密码不能为空");
            return result;
        }
        if(!pwd1.equals(pwd2)) {
            result.put("code", 2);
            result.put("msg", "两次输入的密码不一致，请重新输入");
            return result;
        }
        String md532 = Md5Util.getMd532(pwd1);
        conpaynService.editPassWord(id,md532);
        result.put("code", 0);
        result.put("msg", "密码修改成功，请重新登录");
        return result;
    }

    //注销
    @RequestMapping("remove")
    public String remove(HttpSession session){
        session.removeAttribute(session.getId());
        return "company/login";
    }

    //公司：账户查询
    @RequestMapping("findCompanyAccoumt")
    @ResponseBody
    public ResultBean findCompanyAccoumt(HttpSession session){
        ComUserBean attribute = (ComUserBean) session.getAttribute(session.getId());
        Integer id = attribute.getId();
        return conpaynService.findCompanyAccoumt(id);
    }

    //公司:绑定账号修改 回显
    @RequestMapping("findAccountInfoById")
    @ResponseBody
    public ComTel findAccountInfoById(Integer id){
        return conpaynService.findAccountInfoById(id);
    }

    //公司:绑定账号新增、修改
    @RequestMapping("saveCompanyAccount")
    @ResponseBody
    public void saveCompanyAccount(ComTel comTel,HttpSession session){
        ComUserBean attribute = (ComUserBean) session.getAttribute(session.getId());
        Integer id = attribute.getId();
        comTel.setComId(id);
        conpaynService.saveCompanyAccount(comTel);
    }

    //查询公司信息
    @RequestMapping("findConypanyMessage")
    @ResponseBody
    public ResultBean findConypanyMessage(HttpSession session){
        ComUserBean attribute = (ComUserBean) session.getAttribute(session.getId());
        Integer id = attribute.getId();
        return conpaynService.findConypanyMessage(id);
    }

    //修改公司信息：回显
    @RequestMapping("findCompanyMessageById")
    @ResponseBody
    public CompanyMessage findCompanyMessageById(Integer id){
        return conpaynService.findCompanyMessageById(id);
    }

    //修改公司信息
    @RequestMapping("saveCompanyAccountMessage")
    @ResponseBody
    public Boolean saveCompanyAccountMessage(CompanyMessage companyMessage,HttpSession session){
        ComUserBean attribute = (ComUserBean) session.getAttribute(session.getId());
        Integer id = attribute.getId();
        companyMessage.setComId(id);
        try {
            conpaynService.saveCompanyAccountMessage(companyMessage);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    //查询套餐
    @RequestMapping("findTaoCan")
    @ResponseBody
    public ResultBean findTaoCan(Integer limit,Integer page){
        return conpaynService.findTaoCan(limit,page);
    }

    /**
     * 图片上传
     * @param request
     * @return
     * @throws IOException
     */
    @RequestMapping("upload")
    @ResponseBody
    public Map upload(MultipartFile file, HttpServletRequest request) throws IOException {
        OSSClientUtil ossClient = new OSSClientUtil();
        // 获取上传的文件
        String name = ossClient.uploadImg2Oss(file);
        String fileUpload = ossClient.getImgUrl(name);
        HashMap<String, Object> result = new HashMap<>();
        result.put("img", fileUpload);
        return result;
    }


}
